<template>
	<view class="login-page page">
		<view class="form-block">
			<button hover-class="none" open-type="chooseAvatar" @chooseavatar="chooseAvatar" class="avatar-block">
				<image :src="form.avatar||cloudStorage+'index/user-photo.png'" class="avatar"></image>
				<view class="iconfont icon-xiangji"></view>
			</button>
			<view class="form-item">
				<view class="form-label">昵称</view>
				<input id="nickname-input" class="form-ctrl" v-model="form.name" type="nickname"
					placeholder-class="input-placeholder" placeholder="请输入昵称" maxlength="10" />
			</view>
			<view class="form-item">
				<view class="form-label">手机号码</view>
				<button hover-class="none" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"
					class="getPhone">{{form.phone||'点击获取手机号码'}}</button>
			</view>
		</view>
		<view @click="submit" class="btn normal">保存</view>
	</view>
</template>

<script>
	import {
		validate
	} from '@/util/validate.js'
	import {
		savePhoneNumberByCode,
		saveUserInfo,
		getUserInfo
	} from '@/api/user.js'
	import {
		uploadFile
	} from '@/api/file.js'
	export default {
		data() {
			return {
				type: '',
				form: {
					name: '',
					phone: '',
					avatar: ''
				},
				rule: {
					name: {
						name: '昵称',
						required: true
					},
					avatar: {
						name: '头像',
						required: true
					},
					phone: {
						name: '手机号',
						required: true
					}
				},
			}
		},
		methods: {
			getNickName() {
				return new Promise((resolve, reject) => {
					uni.createSelectorQuery().in(this).select('#nickname-input').fields({
						properties: ["value"]
					}).exec((res) => {
						resolve(res?.[0]?.value)
					})
				})
			},
			async submit() {
				this.form.name = await this.getNickName();
				if (validate(this.form, this.rule)) {
					uni.showLoading({
						title: '保存中',
						mask: true
					})
					saveUserInfo(this.form).then(() => {
						this.$store.dispatch('user/getUser')
						this.showToast('保存成功', () => {
							uni.navigateBack();
						})
					})
				}
			},
			chooseAvatar(e) {
				uploadFile(e.detail.avatarUrl).then(res => {
					this.form.avatar = res;
				})
			},
			async getPhoneNumber(e) {
				if (!e.detail.code) {
					this.showToast('您取消了授权');
					return;
				}
				if (!this.$store.getters.token) {
					let loginCode = await getWxLoginCode()
					uni.showLoading({
						title: '登录中',
						mask: true,
					})
					await this.$store.dispatch('user/getWxLoginCode', loginCode)
				}
				uni.showLoading({
					title: '获取中',
					mask: true
				})
				savePhoneNumberByCode({
					code: e.detail.code
				}).then((res) => {
					this.form.phone = res;
				})
			},
			getInfo() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getUserInfo().then(res => {
					Object.keys(this.form).forEach(item => {
						this.form[item] = res[item]
					})
				})
			}
		},
		onLoad(e) {
			this.getInfo();
		}
	}
</script>

<style lang="scss" scoped>
	.login-page {
		width: 100%;
		padding: 30rpx;

		.form-block {
			background: #fff;
			border-radius: 12px;
			overflow: hidden;

			.avatar-block {
				margin: 60rpx auto;
				width: 160rpx;
				height: 160rpx;
				position: relative;
				padding: 0;

				.avatar {
					width: 100%;
					height: 100%;
					border-radius: 50%;
				}

				.iconfont {
					position: absolute;
					right: 0rpx;
					bottom: 0rpx;
					color: #333;
					font-size: 38rpx;
				}
			}

			.form-item {
				width: 100%;
				height: 100rpx;
				border-bottom: 1px solid #efefef;
				padding: 0 30rpx;
				display: flex;
				align-items: center;
				position: relative;

				.form-label {
					width: 160rpx;
				}

				.form-ctrl {
					width: calc(100% - 160rpx);
				}

				.getPhone {
					width: calc(100% - 220rpx);
					height: 100%;
					position: absolute;
					left: 190rpx;
					z-index: 1;
					display: flex;
					align-items: center;
					font-size: 28rpx;
				}
			}

			.form-item:last-child {
				border: none;
			}
		}

		.btn {
			margin: 60rpx auto;
		}
	}
</style>